package com.blog.controller;

import com.blog.pojo.Article;
import com.blog.pojo.Comment;
import com.blog.service.ArticleService;
import com.blog.service.CommentService;
import com.blog.service.impl.ArticleServiceImpl;
import com.blog.service.impl.CommentServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 文章详情
 */
@WebServlet("/ArticleDetailServlet.do")
public class ArticleDetailServlet extends HttpServlet {

    ArticleService articleService=new ArticleServiceImpl();
    CommentService commentService=new CommentServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //文章ID
        String id = req.getParameter("id");

        //把该文章的阅览数+1
        articleService.updateVisitCount(id);

        //根据ID查询文章  点击数已经+1
        Article article= articleService.getById(id);


        //查询当前文章的上一篇
        Article article_pre=articleService.getPreArticle(article.getId());
        req.setAttribute("article_pre",article_pre);

        //查询当前文章的下一篇
        Article article_next=articleService.getNextArticle(article.getId());
        req.setAttribute("article_next",article_next);

        //加载博文的评论数据
        List<Comment> comment = commentService.findCommentsByArticleId(Integer.valueOf(id));
        req.setAttribute("comment",comment);
        article.setComment(comment.size());
        req.setAttribute("article",article);
        req.getRequestDispatcher("/page/article.jsp").forward(req,resp);
    }
}
